﻿//-----------------------------------------------------------------------
// <copyright file="Image.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.Commerce
{
    using System;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Representaion of an image
    /// </summary>
    [Serializable]
    public class Image : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Image url/path
        /// </summary>
        private string image;

        /// <summary>
        /// Alternative tag
        /// </summary>
        private string alt;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Image class.
        /// </summary>
        public Image()
            : this(Guid.Empty, string.Empty, string.Empty, 0)
        {
        }

        /// <summary>
        /// Initializes a new instance of the Image class.
        /// </summary>
        /// <param name="ownerGuid">Owner guid</param>
        public Image(Guid ownerGuid)
            : this(ownerGuid, string.Empty, string.Empty, 0)
        {         
        }

        /// <summary>
        /// Initializes a new instance of the Image class.
        /// </summary>
        /// <param name="ownerGuid">Owner guid</param>
        /// <param name="imagePath">Image path</param>
        /// <param name="altTag">Alternative tag</param>
        /// <param name="sortOrder">Sort order</param>
        public Image(Guid ownerGuid, string imagePath, string altTag, int sortOrder)
            : base()
        {
            this.image = imagePath;
            this.alt = altTag;
            this.SortOrder = sortOrder;
            this.OwnerGuid = ownerGuid;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the owner guid value.
        /// </summary>
        public virtual Guid OwnerGuid { get; set; }

        /// <summary>
        /// Gets or sets image url
        /// </summary>
        public virtual string ImagePath
        {
            get
            {
                return this.image;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Image path must be a minimum of 1 characters.");
                }

                if (value.Length > 100)
                {
                    throw new InvalidLengthException("Image path must be a maximum of 100 characters.");
                }

                this.image = value;
            }
        }

        /// <summary>
        /// Gets or sets the alternative image tag
        /// </summary>
        public virtual string AltTag
        {
            get
            {
                return this.alt;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Image alt tag must be a minimum of 1 characters.");
                }

                if (value.Length > 100)
                {
                    throw new InvalidLengthException("Image alt tag name must be a maximum of 100 characters.");
                }

                this.alt = value;
            }
        }

        /// <summary>
        /// Gets or sets the display order
        /// </summary>
        public virtual int SortOrder { get; set; }
        #endregion

        #region Load Methods
        /// <summary>
        /// Load image by unique identifier
        /// </summary>
        /// <param name="guid">Unique identifier</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Filled Image</returns>
        public static Image Load(Guid guid, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", guid));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetSingle<Image>("LoadImage", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Delete Methods
        /// <summary>
        /// Delete image from database
        /// </summary>
        public virtual void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            DataAccess.NonQuery("DeleteImage", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Save Methods
        /// <summary>
        /// Save image to database
        /// </summary>
        public virtual void Save()
        {
            DataAccess.NonQuery("SaveImage", System.Data.CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Fill Methods
        /// <summary>
        /// Fill object from database
        /// </summary>
        /// <param name="reader">Data from database</param>
        public virtual void Fill(System.Data.IDataReader reader)
        {
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.image = Helper.ToString(reader["Image"]);
            this.OwnerGuid = Helper.ToGuid(reader["OwnerGuid"]);
            this.SortOrder = Helper.ToInt(reader["SortOrder"]);
            this.alt = Helper.ToString(reader["Alt"]);
        }
        #endregion

        #region Get Save Parameters
        /// <summary>
        /// Get save parameters
        /// </summary>
        /// <returns>Parameter collection</returns>
        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("OwnerGuid", this.OwnerGuid));
            parameters.Add(new Parameter("Image", this.ImagePath));
            parameters.Add(new Parameter("Alt", this.AltTag));
            parameters.Add(new Parameter("SortOrder", this.SortOrder));
            return parameters;
        }
        #endregion
    }
}
